package com.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.score.mapper.ICheckDormMapper;
import com.score.mapper.IDormMapper;
import com.score.pojo.ReturnEntry;
import com.score.pojo.ResponseEntry;
import com.score.pojo.TCheckDorm;
import com.score.pojo.TDorm;
import com.score.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class CheckDormServiceImpl {

    @Autowired
    private ICheckDormMapper checkDormMapper;

    @Autowired
    private IDormMapper dormMapper;

    /**
     * 获取所有寝室信息
     *
     * @return
     */
    public ReturnEntry getAllDorm(String college, String className, String position) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        if (position.equals("headmaster")) {
            wrapper.eq("class_name", className);
        } else {
            wrapper.eq("college", college);
        }
        List<TCheckDorm> checkDormList = checkDormMapper.selectList(wrapper);
        Set<String> classSet = new HashSet<>();
        for (int i = 0; i < checkDormList.size(); i++) {
            classSet.add(checkDormList.get(i).getClassName());
        }
        return new ReturnEntry().ok(checkDormList, classSet);
    }

    /**
     * 老师页面获取所有查寝时间
     *
     * @return
     */
    public Set<String> getAllTime(String college, String className, String position) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        if (position.equals("headmaster")) {
            wrapper.eq("class_name", className)
                    .select("check_time", "class_name")
                    .groupBy("check_time");
        } else {
            wrapper.eq("college", college)
                    .select("check_time", "college")
                    .groupBy("check_time");
        }
        List<TCheckDorm> tCheckDorms = checkDormMapper.selectList(wrapper);
        Set<String> set = new TreeSet<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < tCheckDorms.size(); i++)
            set.add(sdf.format(tCheckDorms.get(i).getCheckTime()));
        return set;

    }

    /**
     * 学生获取学生寝室所有查寝信息
     *
     * @param dormNum
     * @return
     */
    public Page<TCheckDorm> getStuDorm(String dormNum, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        wrapper.eq("dorm_num", dormNum);
        return checkDormMapper.selectPage(new Page<>(currentPage,pageSize),wrapper);
    }

    /**
     * 老师修改寝室信息状态
     *
     * @param dorm
     * @return
     */
    public TCheckDorm changeReadDorm(TCheckDorm dorm, String position) {
        if (!position.equals("headmaster"))
            dorm.setFdyReadIt("read");
        else
            dorm.setReadIt("read");
        int i = checkDormMapper.updateById(dorm);
        if (i==1)
        return dorm;
        else
            throw new RuntimeException("插入错误");
    }

    /**
     * 老师修改寝室合格信息
     *
     * @param dorm
     * @return
     */
    public TCheckDorm changeStateDorm(TCheckDorm dorm) {
        dorm.setState("合格");
        checkDormMapper.updateById(dorm);
        return dorm;
    }

    /**
     * 根据寝室号获取查寝时间
     *
     * @return
     */
    public Set<String> getAllStuTime(String dormNum) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isNullOrEmpty(dormNum),"dorm_num", dormNum);
        List<TCheckDorm> tCheckDorms = checkDormMapper.selectList(wrapper);
        Set<String> set = new TreeSet<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (TCheckDorm tCheckDorm : tCheckDorms)
            set.add(sdf.format(tCheckDorm.getCheckTime()));
        return set;
    }

    /**
     * 学生改变寝室信息读取状态
     *
     * @param dorm
     * @return
     */
    public ResponseEntry changeStateStu(TCheckDorm dorm) {
        dorm.setStuReadIt("read");
        int i = checkDormMapper.updateById(dorm);
        if (i == 1)
            return new ResponseEntry().ok("已读", i);
        return new ResponseEntry().fail("已读失败", i);
    }

    /**
     * 学生反馈给老师信息
     *
     * @param dorm
     * @return
     */
    public ResponseEntry sendFeedback(TCheckDorm dorm) {
        dorm.setFeedbackReadIt("unread");
        dorm.setFdyFeedbackReadIt("unread");
        int i = checkDormMapper.updateById(dorm);
        ResponseEntry entry = new ResponseEntry();
        if (i == 1)
            return entry.ok("发送成功", i);
        return entry.fail("发送失败", i);
    }

    /**
     * 老师查看反馈信息，改变反馈信息状态
     *
     * @param dorm
     * @return
     */
    public TCheckDorm changeFbRead(TCheckDorm dorm, String position) {
        if (!position.equals("headmaster"))
            dorm.setFdyFeedbackReadIt("read");
        else
            dorm.setFeedbackReadIt("read");
        checkDormMapper.updateById(dorm);
        return dorm;
    }

    /**
     * 老师页面筛选所有寝室信息
     *
     * @param check_time
     * @param state
     * @param isread
     * @return
     */
    public ReturnEntry findBycondition(String college, String className, String position, String check_time, String state, String isread, Integer currentPage, Integer pageSize) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        QueryWrapper<TCheckDorm> bzr = wrapper.likeRight(!StringUtils.isNullOrEmpty(check_time), "check_time", check_time)
                .eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                .eq(!StringUtils.isNullOrEmpty(state), "state", state);
        //辅导员查寝
        if (!position.equals("headmaster")) {
            if (StringUtils.isNullOrEmpty(isread)) {
                bzr.eq(!StringUtils.isNullOrEmpty(college), "college", college);
            } else if (isread.equals("read")) {
                bzr.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_read_it", isread)
                        .eq("fdy_feedback_read_it", isread);
            } else {
                bzr.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_read_it", isread)
                        .or()
                        .likeRight(!StringUtils.isNullOrEmpty(check_time), "check_time", check_time)
                        .eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                        .eq(!StringUtils.isNullOrEmpty(state), "state", state)
                        .eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_feedback_read_it", isread);
            }
        }
        //班主任查寝
        else {
            if (isread.equals("read")) {
                bzr.eq(!StringUtils.isNullOrEmpty(isread), "read_it", isread)
                        .eq(!StringUtils.isNullOrEmpty(isread), "feedback_read_it", isread);
            } else if (isread.equals("unread")) {
                bzr.eq(!StringUtils.isNullOrEmpty(isread), "read_it", isread)
                        .or()
                        .likeRight(!StringUtils.isNullOrEmpty(check_time), "check_time", check_time)
                        .eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                        .eq(!StringUtils.isNullOrEmpty(state), "state", state)
                        .eq(!StringUtils.isNullOrEmpty(isread), "feedback_read_it", isread);
            }
        }
        Page<TCheckDorm> tCheckDormPage = checkDormMapper.selectPage(new Page<>(currentPage, pageSize), bzr);
        return new ReturnEntry().ok(tCheckDormPage.getRecords(), getAllDorm(college, className, position).getData(), getAllTime(college, className, position), tCheckDormPage.getTotal() + "");
    }

    /**
     * 筛选学生自己寝室信息
     *
     * @param dormNum
     * @param check_time
     * @param state
     * @return
     */
    public ReturnEntry findBySome(String dormNum, String check_time, String state, Integer currentPage, Integer pageSize) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        wrapper.likeRight(!StringUtils.isNullOrEmpty(check_time), "check_time", check_time)
                .eq(!StringUtils.isNullOrEmpty(state), "state", state)
                .eq(!StringUtils.isNullOrEmpty(dormNum), "dorm_num", dormNum);
        Page<TCheckDorm> tCheckDormPage = checkDormMapper.selectPage(new Page<>(currentPage, pageSize), wrapper);
        return new ReturnEntry().ok(tCheckDormPage.getRecords(), tCheckDormPage.getTotal());
    }

    public ReturnEntry allRead(String college, String className, String position) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        QueryWrapper<TCheckDorm> unpass = new QueryWrapper<>();
        QueryWrapper<TCheckDorm> unpass2 = new QueryWrapper<>();
        if (position.equals("headmaster")) {
            wrapper.eq("class_name", className)
                    .eq("state", "优秀")
                    .eq("state", "良好")
                    .eq("state", "一般")
                    .eq("read_it", "unread");
            for (TCheckDorm checkdorm :
                    checkDormMapper.selectList(wrapper)) {
                checkdorm.setReadIt("read");
                checkDormMapper.updateById(checkdorm);
            }
            unpass.eq("class_name", className)
                    .eq("state", "较差")
                    .eq("state", "脏乱差")
                    .eq("read_it", "unread");
            unpass2.eq("class_name", className)
                    .eq("feedback_read_it", "unread");
        } else {
            wrapper.eq("college", college)
                    .eq("state", "优秀")
                    .eq("state", "良好")
                    .eq("state", "一般")
                    .eq("fdy_read_it", "unread");
            for (TCheckDorm checkdorm :
                    checkDormMapper.selectList(wrapper)) {
                checkdorm.setFdyReadIt("read");
                checkDormMapper.updateById(checkdorm);
            }
            unpass.eq("college", college)
                    .eq("state", "较差")
                    .eq("state", "脏乱差")
                    .eq("fdy_read_it", "unread");
            unpass2.eq("college", college)
                    .eq("fdy_feedback_read_it", "unread");
        }
        return new ReturnEntry().ok("一键已读成功", checkDormMapper.selectList(unpass).size() + checkDormMapper.selectList(unpass2).size());
    }

    public ReturnEntry getStateAndCount(String college, String className, String state, Integer currentPage, Integer pageSize) {
        QueryWrapper<TCheckDorm> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isNullOrEmpty(state), "state", state)
                .eq(!StringUtils.isNullOrEmpty(college), "college", college)
                .eq(!StringUtils.isNullOrEmpty(className), "class_name", className);
        List<TCheckDorm> checkDormList = checkDormMapper.selectList(wrapper);
        Set<String> set = new HashSet<>();//寝室号
        for (TCheckDorm checkDorm : checkDormList) {
            set.add(checkDorm.getDormNum());
        }
        QueryWrapper<TCheckDorm> classQW = new QueryWrapper<>();
        QueryWrapper<TDorm> dormQW = new QueryWrapper<>();
        List<String> nameList = new ArrayList<>(20);//寝室人员名字
        List<ReturnEntry> stuList = new ArrayList<>(set.size());
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String dormnum : set) {
            classQW.clear();
            classQW.eq("dorm_num", dormnum)
                    .eq(!StringUtils.isNullOrEmpty(state), "state", state);
            dormQW.clear();
            dormQW.eq("dorm_num", dormnum);
            List<TDorm> dormList = dormMapper.selectList(dormQW);
            for (TDorm numname : dormList) {
                sb.append(numname.getName() + ",");
            }
            nameList.add(sb.substring(0, sb.length() - 1));
            stuList.add(new ReturnEntry(i, dormnum, nameList.get(i), state, checkDormMapper.selectCount(classQW) + ""));
            sb.delete(0, sb.length());
            i++;
        }
        List list = PageUtil.startPage(stuList, currentPage, pageSize);
        return new ReturnEntry().ok(list, stuList.size(),stuList,"筛选结果");

    }

    public ReturnEntry getDetailedMsg(String dormNum, String state, Integer currentPage, Integer pageSize) {
        QueryWrapper<TCheckDorm> checkDormQueryWrapper = new QueryWrapper<>();
        checkDormQueryWrapper.eq(!StringUtils.isNullOrEmpty(dormNum),"dorm_num",dormNum)
                .eq(!StringUtils.isNullOrEmpty(state),"state",state);
        Page<TCheckDorm> tCheckDormPage = checkDormMapper.selectPage(new Page<>(currentPage, pageSize), checkDormQueryWrapper);
        return new ReturnEntry().ok(tCheckDormPage.getRecords(),tCheckDormPage.getTotal());
    }
}